<template>
  <div>
    <el-button type="primary" @click="visible = true" icon="el-icon-circle-plus-outline">新增</el-button>
    <el-table v-loading="loading" :data="dataSource" border>
      <el-table-column prop="licenseName" label="授权名称" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="effectTime" label="生效日期" align="center"></el-table-column>
      <el-table-column prop="expiryTime" label="过期日期" align="center"></el-table-column>
      <el-table-column prop="dataState" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dataState >=0 ">正常</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="150px">
        <template slot-scope="scope">
          <el-button type="success" icon="el-icon-edit" circle @click="() => { entityInfo=$Util.copy(scope.row);entityInfo.checkTags=$Util.toArray(entityInfo.checkTags);visible=true}" title="编辑"></el-button>
          <el-button type="info" icon="el-icon-download" circle @click="download(scope.row)" title="下载"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>

    <el-dialog :visible.sync="visible" title="授权信息" append-to-body>
      <el-form label-width="90px" label-position="right">
        <el-form-item label="授权名称" required>
          <el-input v-model="entityInfo.licenseName"></el-input>
        </el-form-item>
        <el-form-item label="生效日期" required>
          <el-date-picker v-model="entityInfo.effectTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%"></el-date-picker>
        </el-form-item>
        <el-form-item label="过期日期" required>
          <el-date-picker v-model="entityInfo.expiryTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%"></el-date-picker>
        </el-form-item>
        <el-form-item label="校验信息" required>
          <el-select v-model="entityInfo.checkTags" multiple placeholder="请选择" style="width: 100%">
            <el-option v-for="item in checkTags" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <template v-for="item in checkTags">
          <el-form-item v-if="entityInfo.checkTags && entityInfo.checkTags.includes(item.value)" :label="item.label" :key="item.value" required>
            <el-input v-model="entityInfo[item.value]"></el-input>
          </el-form-item>
        </template>
        <el-form-item label="状态">
          <el-radio-group v-model="entityInfo.dataState">
            <el-radio :label="-1">禁用</el-radio>
            <el-radio :label="0">正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item style="text-align: center">
          <el-button icon="el-icon-circle-check" type="success" @click="saveLicense">{{ entityInfo.licenseId ? '更新' : '保存' }}</el-button>
          <el-button icon="el-icon-circle-close" type="info" @click="visible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";

export default {
  name: "License",
  mixins: [IdeaTable],
  data() {
    return {
      checkTags: [
          { value: 'ipAddress', label: 'Ip地址' },{ value: 'macAddress', label: 'MAC地址' },{ value: 'cpuSerial', label: 'cpu型号' },
          { value: 'mainBoardSerial', label: '主板型号' },{ value: 'userName', label: '授权用户' },{ value: 'osName', label: '操作系统' }
      ]
    }
  },
  created() {
    this.queryUrl = '/license/queryPage'
    this.removeUrl = '/license/delete/'
    this.saveUrl = '/license/save'
    this.rowKey = 'licenseId'
    this.pageInfo.pageSize = 15
    this.loadSource()
  },
  methods: {
    initEntity(){
      this.entityInfo = { dataState: 0, checkTags: [] }
    },
    saveLicense() {
      const data = this.$Util.copy(this.entityInfo)
      data.checkTags = this.$Util.toJsonStr(data.checkTags)
      this.saveData(data)
    },
    download(row) {
      const accessToken = this.$AuthStorage.getAccessToken()
      const url = this.$Util.mergeUrl(process.env.VUE_APP_BASE_API,
          `/license/download/${row.licenseId}?access_token=${accessToken}`)
      window.open(url)
    }
  }
}
</script>

<style scoped>

</style>
